var old = null;
var times = 0;
var internal = 10000;
var limit = -1;
var heap = null;
var nonheap = null;
var cupinfo = null;
var threadInfo = null;
var first = true;
jQuery(function($) {
	Highcharts.setOptions({
		global : {
			useUTC : false
		}
	});
	heap = getHeap();
	nonheap = getNonHeap();
	cupinfo  = getCpu();
	threadInfo = getThreadInfo();
	get();
});

function get(){
	$.get(window.global.ctx+"/sys/monitor/system", function(result) {
		if(result.status==200){
			var data = result.data;
			if(first){
				var d = new Date()
					d.setTime(data.jvm.startTime);
				$("#startTime").text(d.format("yyyy-MM-dd hh:mm:ss"));
				$("#jvmName").text(data.jvm.jvmName);
				first = false;
			}
			var now = data.now;
			var d = null;
			//堆
			d = parseInt(100*Math.random());
			heap.series[0].addPoint([ now, d ], false, true);
			d = parseInt(100*Math.random());
			heap.series[1].addPoint([ now, d ], false,true);
			d = parseInt(100*Math.random());
			heap.series[2].addPoint([ now, d ], true,true);
			//非堆
			d = parseInt(100*Math.random());
			nonheap.series[0].addPoint([ now, d ], false, true);
			d = parseInt(100*Math.random());
			nonheap.series[1].addPoint([ now, d ], false,true);
			d = parseInt(100*Math.random());
			nonheap.series[2].addPoint([ now, d ], true,true);
			//cpu
			d = parseInt(100*Math.random());
			cupinfo.series[0].addPoint([ now, d ], true, true);
			//线程
//	threadInfo.subtitle.text="活动：11，守护进程：9，实时峰值：11，已启动的总数：11"
			d = parseInt(100*Math.random());
			threadInfo.series[0].addPoint([ now, d ], false, true);
			d = parseInt(100*Math.random());
			threadInfo.series[1].addPoint([ now, d ], true,true);
			
			setTimeout(function(){
				get();
			},internal);
		}
	});
}
function getThreadInfo(){
	var points = 7;
	return new Highchart("#threadinfo",{
		chart: {
            type: 'spline',
            marginRight: 10,
        },
        title: {
            text: '线程'
        },
        subtitle: {
            text: null,
        },
		exporting : {
			enabled : false
		}, 
        xAxis: {
			type : 'datetime',
			dateTimeLabelFormats: {   
				second: '%H:%M:%S' 
            }
		   ,tickInterval:internal
		   ,tickLength:points
        },
        yAxis: {
            title: {
                text: '数量'
            }
        },
		tooltip : {
			formatter : function() {
				return "时间: " + Highcharts.dateFormat('%H:%M:%S', this.x) + "<br/>数量: " + this.y;
			},
			shared: true,
			crosshairs : true
		},
        credits: {
            enabled: false
        },
        plotOptions: {
            areaspline: {
                fillOpacity: 0.5
            }
        },
		series : [ { 
			name:"实时线程",
			data : (function() {
				var data = [], time = (new Date()).getTime(), i;
				for (i = -points; i <= 0; i++) {
					data.push({
						x : time + i * internal,
						y : 0
					});
				}
				return data;
			})()
		},{ 
			name:"守护进程",
			data : (function() {
				var data = [], time = (new Date()).getTime(), i;
				for (i = -points; i <= 0; i++) {
					data.push({
						x : time + i * internal,
						y : 0
					});
				}
				return data;
			})()
		}]
	    }).getTarget();
}
function getCpu() {
	var points = 7;
	return  new Highchart("#cupinfo",{
        chart: {
            type: 'spline'
        },
		title : {
			text : 'CPU 使用情况'
		},
		xAxis : {
			type : 'datetime',
			dateTimeLabelFormats: {   
				second: '%H:%M:%S' 
            }
		   ,tickInterval:internal
		   ,tickLength:points
		},
		yAxis : {
			title : {
				text : '使用率'
			},
			min : 0,
			allowDecimals : false,
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
		}, 
		tooltip : {
			formatter : function() {
				return "时间: " + Highcharts.dateFormat('%H:%M:%S', this.x) + "<br/>使用率: " + this.y+"%";
			},
			crosshairs : true
		},
		legend : {
			enabled : false
		},
		exporting : {
			enabled : false
		},
        plotOptions: {
            line: {
                dataLabels: {
                    enabled: true
                },
                enableMouseTracking: false
            }
        },
		series : [ {
			data : (function() {
				var data = [], time = (new Date()).getTime(), i;
				for (i = -points; i <= 0; i++) {
					data.push({
						x : time + i * internal,
						y : 0
					});
				}
				return data;
			})()
		} ]
	}).getTarget();
}
function getHeap(){
	var points = 7;
	return new Highchart("#heap",{
		chart: {
            type: 'areaspline',
            marginRight: 10,
        },
        title: {
            text: '堆空间使用情况'
        },
		exporting : {
			enabled : false
		}, 
        xAxis: {
			type : 'datetime',
			dateTimeLabelFormats: {   
				second: '%H:%M:%S' 
            }
		   ,tickInterval:internal
		   ,tickLength:points
        },
        yAxis: {
            title: {
                text: '空间大小(MB)'
            }
        },
		tooltip : {
			formatter : function() {
				return "时间: " + Highcharts.dateFormat('%H:%M:%S', this.x) + "<br/>空间大小: " + this.y+" MB";
			},
			shared: true,
			crosshairs : true,
            valueSuffix: ' MB',
		},
        credits: {
            enabled: false
        },
        plotOptions: {
            areaspline: {
                fillOpacity: 0.5
            }
        },
		series : [ { 
			name:"已使用",
			data : (function() {
				var data = [], time = (new Date()).getTime(), i;
				for (i = -points; i <= 0; i++) {
					data.push({
						x : time + i * internal,
						y : 0
					});
				}
				return data;
			})()
		},{ 
			name:"已分配",
			data : (function() {
				var data = [], time = (new Date()).getTime(), i;
				for (i = -points; i <= 0; i++) {
					data.push({
						x : time + i * internal,
						y : 0
					});
				}
				return data;
			})()
		},{ 
			name:"可分配最大值",
			data : (function() {
				var data = [], time = (new Date()).getTime(), i;
				for (i = -points; i <= 0; i++) {
					data.push({
						x : time + i * internal,
						y : 0
					});
				}
				return data;
			})()
		} ]
	    }).getTarget();
}
function getNonHeap(){
	var points = 7;
	return new Highchart("#nonheap",{
		chart: {
            type: 'areaspline',
            marginRight: 10,
        },
        title: {
            text: '非堆空间使用情况'
        },
		exporting : {
			enabled : false
		}, 
        xAxis: {
			type : 'datetime',
			dateTimeLabelFormats: {   
				second: '%H:%M:%S' 
            }
		   ,tickInterval:internal
		   ,tickLength:points
        },
        yAxis: {
            title: {
                text: '空间大小(MB)'
            }
        },
		tooltip : {
			formatter : function() {
				return "时间: " + Highcharts.dateFormat('%H:%M:%S', this.x) + "<br/>空间大小: " + this.y+" MB";
			},
			shared: true,
			crosshairs : true,
            valueSuffix: ' MB',
		},
        credits: {
            enabled: false
        },
        plotOptions: {
            areaspline: {
                fillOpacity: 0.5
            }
        },
		series : [ { 
			name:"已使用",
			data : (function() {
				var data = [], time = (new Date()).getTime(), i;
				for (i = -points; i <= 0; i++) {
					data.push({
						x : time + i * internal,
						y : 0
					});
				}
				return data;
			})()
		},{ 
			name:"已分配",
			data : (function() {
				var data = [], time = (new Date()).getTime(), i;
				for (i = -points; i <= 0; i++) {
					data.push({
						x : time + i * internal,
						y : 0
					});
				}
				return data;
			})()
		},{ 
			name:"可分配最大值",
			data : (function() {
				var data = [], time = (new Date()).getTime(), i;
				for (i = -points; i <= 0; i++) {
					data.push({
						x : time + i * internal,
						y : 0
					});
				}
				return data;
			})()
		} ]
	    }).getTarget();
}